<template>
  <div>
    <div class="page">
      <!-- <iframe
        class="wxPayUrl"
        :src="wxPayUrl"
        sandbox="allow-scripts allow-top-navigation allow-same-origin"
      ></iframe>-->
    </div>
  </div>
</template>
<script>
  import Pay from '@/dll/pay'

  export default {
    name: 'pay',
    data () {
      return {
        wxPayUrl: '',
        isOrder: false
      }
    },
    created () {
      this.isOrder = this.$route.query.isOrder
      this.init()
    },
    methods: {
      init () {
        this.out_trade_no = this.$route.params.id
        let wxPayUrl = this.$route.params.wxPayUrl
        if (wxPayUrl) {
          location.href = wxPayUrl
          // location.href= wxPayUrl + "&redirect_url=" + encodeURIComponent(location.href);
          setTimeout(() => {
            this.checkPay()
          }, 1000)
        } else {
          this.checkPay()
        }
      },
      back () {
        if (this.isOrder == 'true') {
          this.$router.back()
        } else {
          this.$router.replace({
            name: 'mallOrder',
            params: { tabIndex: 1 }
          })
        }
      },
      checkPay () {
        this.$vux.confirm.show({
          title: '支付',
          content: '是否已支付？',
          cancelText: '未支付',
          confirmText: '已支付',
          // 组件除show外的属性
          onCancel: () => {
            this.back()
            // 取消
          },
          onConfirm: () => {
            this.payQuery()
            // 确定
          }
        })
      },
      async payQuery () {
        let param = {
          out_trade_no: this.out_trade_no,
          pay_type: 'MWEB'
        }

        this.$vux.loading.show({
          text: '请稍后'
        })
        let res = await Pay.wxPayOrderState(param)
        this.$vux.loading.hide()

        if (res.code == 0) {
          this.$vux.alert.show({
            title: '提示',
            content: '支付成功',
            onHide: () => {
              if (res.data.pay_where == 1) {
                this.$router.replace({
                  name: 'mallGroupWin',
                  query: { order_sn: res.data.order_sn, isOrder: this.isOrder }
                })
              } else {
                this.back()
              }
            }
          })
        } else {
          this.$vux.alert.show({
            title: '提示',
            content: res.msg,
            onHide: () => {
              this.back()
            }
          })
        }
      }
    }
  }
</script>
<style lang="less" scoped>
  .wxPayUrl {
    margin: 0;
    padding: 0;
    height: 100vh;
    width: 100vw;
    border: none;
  }
</style>
